package com.easonji.common.constant.dictmap.factory;

import java.lang.reflect.Method;

import com.easonji.common.util.SpringContextHolder;
import com.easonji.common.util.StringUtils;

/**
 * @ClassName: DictFieldWarpperFactory
 * @Description: 字段的包装创建工厂
 * @Author: LiuJun
 * @date: 2018/07/18 17:39
 * @Version: 1.0
 * @Copyright: Copyright (c) 2018 - 2019
 */
public class DictFieldWarpperFactory {

    public static Object createFieldWarpper(Object field, String classAndMenthod) {
        String[] classAndMenthods = classAndMenthod.split("\\.");
        if (StringUtils.isEmpty(classAndMenthods)) {
            return "";
        }
        String className = classAndMenthods[0];
        String methodName = classAndMenthods[1];
        Object object = SpringContextHolder.getBean(StringUtils.firstCharToLowerCase(className) + "Impl");
        try {
            Method method = object.getClass().getMethod(methodName, field.getClass());
            Object result = method.invoke(object, field);
            return result;
        } catch (Exception e) {
            try {
                Method method = object.getClass().getMethod(methodName, Integer.class);
                Object result = method.invoke(object, Integer.parseInt(field.toString()));
                return result;
            } catch (Exception e1) {
                return "";
            }
        }
    }
}
